package com.maotai.llzhou.timetask;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.maotai.llzhou.bean.MaotaiWebCookie;
import com.maotai.llzhou.service.MaotaiService;
import com.maotai.llzhou.thread.MaotaiYuYueThread512Test;
import com.maotai.llzhou.util.JSUtil;

/**
 * 登录并且抢购
 */
public class MaotaiYuYueTask512Test {

	@Resource(name = "maotaiService")
	private MaotaiService maotaiService;

	public void beginTask() {
		List<Map<String, Object>> list;
//		List<Map<String, Object>> addressList;
		try {
			list = maotaiService.getZhanghaoYuYueNew512();
//			List<Map<String, Object>> shenfenList = maotaiService.getShenFengList();
			//token跟账号对应了，从zhanghaoweb表中去获取
//			List<Map<String, Object>> tokenList = maotaiService.getTokenList();
//			addressList = maotaiService.getAddressLibrary();
			if (list != null && list.size() > 0) {
				//因为现在每次只去一个账号，所以在线程中去取ip
//				JSONArray jArray = IPFactory.getIpAndPort();
//				if(jArray.size()<count){
//					count=jArray.size();
//				}
				// 增加代理
//				Map<String, Object> zhanghao0 = list.get(0);
//				String provinceName = zhanghao0.get("provinceName")+"";
//	            String cityName = zhanghao0.get("cityName")+"";
//	            System.out.println(provinceName+"----"+cityName+"----账号数量----"+list.size());
//	            String zmpro = zhanghao0.get("zmpro")+"";
//	            String zmcity = zhanghao0.get("zmcity")+"";
//	            System.out.println("获取到的ip城市为:"+zmpro+"----"+zmcity);
//				//http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=1&time=100&pro=江苏省&city=南京市&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=
////				String httpUrl = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=200&time=100&pro="+provinceName+"&city="+cityName+"&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
////				String httpUrl = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=200&time=100&pro="+provinceName+"&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
////				String httpUrl = "http://ip.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&qty=20&time=100&port=1&format=json&ss=5&css=&dt=1&specialTxt=3&specialJson=";
//	            String httpUrl = "http://webapi.http.zhimacangku.com/getip?num=1&type=2&pro="+zmpro+"&city="+zmcity+"&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
//	            JSONObject jObject = IPFactory
//						.getKdByUrl(httpUrl);
//				if(!jObject.getBoolean("success")){
//					System.out.println("--未获取到ip--"+jObject.getString("msg"));
//					//去掉市限制
//					httpUrl = "http://webapi.http.zhimacangku.com/getip?num=1&type=2&pro="+zmpro+"&city=0&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
//					jObject = IPFactory
//							.getKdByUrl(httpUrl);
//				}
//				if(!jObject.getBoolean("success")){
//					httpUrl = "http://webapi.http.zhimacangku.com/getip?num=1&type=2&pro=0&city=0&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=45&mr=2&regions=";
//					jObject = IPFactory
//							.getKdByUrl(httpUrl);
//				}
//				JSONArray jArray2 = jObject.getJSONArray("data");
				int count = list.size();
//				if(jArray2.size()<count){
//					count=jArray2.size();
//				}
//				if(addressList.size()<count){
//					count=addressList.size();
//				}
//				
//				if(shenfenList.size()<count){
//					count=shenfenList.size();
//				}
//				JSONObject jj = (JSONObject)jArray2.get(0);
//				String ipAndPort = jj.getString("ip")+":"+jj.getString("port");
//				System.out.println("获取到的ip为:"+ipAndPort);
				for(int i=0;i<count;i++){
					Map<String, Object> zhanghao = list.get(i);
//					Map<String, Object> shenfenMap  = shenfenList.get(i);
//					Map<String, Object> tokenMap = tokenList.get(i);
//					Map<String, Object> addrMap = addressList.get(i);
//					JSONObject jj = (JSONObject)jArray.get(i);
//					JSONObject jj = (JSONObject)jArray2.get(i);
//					String ipAndPort = jj.getString("ip")+":"+jj.getString("port");
//					String ipAndPort = jj.getString("IP")+":"+jj.getString("Port");
//					shenfenMap.put("usestatus", "1");
//					maotaiService.updateShenFenStatus(shenfenMap);
					zhanghao.put("yuyuestatus", "1");
					maotaiService.updateZhanghaoYuYueStatus512(zhanghao);
//					addrMap.put("usestatus", "1");
//					maotaiService.updateAddressLibraryStatus(addrMap);
					MaotaiWebCookie mwc = new MaotaiWebCookie();
					String kcross = zhanghao.get("kcross")+"";
					String acw_tc = zhanghao.get("acw_tc")+"";
					String csrfToken = zhanghao.get("csrfToken")+"";
					mwc.setKcross(kcross);
					mwc.setAcw_tc(acw_tc);
					mwc.setCsrfToken(csrfToken);
					String auth = zhanghao.get("auth")+"";
					mwc.setAuth(auth);
					String timestamp = System.currentTimeMillis()+"";
					String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
					mwc.setSign(sign);
					mwc.setTimestamp(timestamp);
					MaotaiYuYueThread512Test myyt = new MaotaiYuYueThread512Test(maotaiService,zhanghao,mwc);
//					MaotaiYuYueThread myyt = new MaotaiYuYueThread(maotaiService,zhanghao,ipAndPort,mwc,tokenMap);
//					MaotaiYuYueThread92 myyt = new MaotaiYuYueThread92(maotaiService,zhanghao,ipAndPort,mwc,tokenMap);
					myyt.start();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
